/**
 * 
 */
package com.autosalon.ui.spravaPujcek;

import java.util.ArrayList;
import java.util.List;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.model.Pujcka;
import com.autosalon.tools.IPaging;
import com.autosalon.tools.PagingComponent;
import com.autosalon.ui.ContentSwitcher;
import com.autosalon.ui.spravaPujcek.action.BackButtonAction;
import com.autosalon.ui.spravaPujcek.action.DeletePujckaAction;
import com.autosalon.ui.spravaPujcek.action.EditPujckaAction;
import com.autosalon.ui.spravaPujcek.action.NewPujckaAction;
import com.autosalon.ui.spravaPujcek.action.SavePujckaAction;
import com.autosalon.ui.spravaPujcek.state.SpravaPujcekStateHolder;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

/**
 * @author Ragisel
 * 
 */
public class SpravaPujcek extends HorizontalLayout implements IPaging {

	private final ContentSwitcher switcher;
	private VerticalLayout contentLayout;
	private HorizontalLayout buttonLayout;
	private List<Object> listPujcky;
	private PagingComponent pagingCompPujcky;
	private Button btnBack;
	private Button btnSave;
	private Button btnNewPujcka;
	private Table tblPujcky;
	private Form formNewPujcka;
	private TextField tfMesicniSplatka;
	private PopupDateField popupKonec;
	private PopupDateField popupPocatek;
	private ComboBox cbBanka;
	private Pujcka pujcka;
	private int id;
	private Long id_pujcky;

	public SpravaPujcek(ContentSwitcher switcher) {
		this.switcher = switcher;
		init();
	}

	private void init() {
		try {
            setId(getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue());
        } catch (NullPointerException e) {
            setId(1);
        }
		setId_pujcky(null);
		initNewPujckaForm();
		contentLayout = new VerticalLayout();
		buttonLayout = new HorizontalLayout();
		btnNewPujcka = new Button("Nová půjčka");
		btnNewPujcka.addListener(new NewPujckaAction(this));
		
		
		pagingCompPujcky = new PagingComponent(this);

		IndexedContainer ic = new IndexedContainer();
		ic.addContainerProperty("BANKA", String.class, null);
		ic.addContainerProperty("MĚSÍČNÍ SPLÁTKA", String.class, null);
		ic.addContainerProperty("POČÁTEČNÍ DATUM", String.class, null);
		ic.addContainerProperty("KONCOVÉ DATUM", String.class, null);
		ic.addContainerProperty("UPRAVIT", Button.class, null);
		ic.addContainerProperty("SMAZAT", Button.class, null);
		tblPujcky = new Table("Půjčky");
		tblPujcky.setContainerDataSource(ic);
		tblPujcky.setSizeFull();
		naplnTabulkuObjednavky();

		buttonLayout.addComponent(btnNewPujcka);
		
		contentLayout.addComponent(pagingCompPujcky);
		contentLayout.addComponent(tblPujcky);
		contentLayout.addComponent(buttonLayout);
		
		addComponent(contentLayout);
		SpravaPujcekStateHolder.setState(SpravaPujcekStateHolder.VIEW_PUJCKY_STATE, this.getId(), this);

	}

	@Override
	public void naplnTabulkuObjednavky() {
		listPujcky = new ArrayList<Object>();
		tblPujcky.removeAllItems();
		listPujcky =  DaoFactory.getinstance().getPujcka().getAll(pagingCompPujcky.getFrom(), pagingCompPujcky.getTo());
		for (Object o : listPujcky) {
			pujcka = (Pujcka)o;
					
			Button btnEditPujcka;
			btnEditPujcka = new Button("Editovat");
			btnEditPujcka.setStyleName(BaseTheme.BUTTON_LINK);
			btnEditPujcka.addListener(new EditPujckaAction(this));
			btnEditPujcka.setData(pujcka.getId());
			
			Button btnDelPujcka;
			btnDelPujcka = new Button("Smazat");
			btnDelPujcka.addListener(new DeletePujckaAction(this));
			btnDelPujcka.setStyleName(BaseTheme.BUTTON_LINK);
			btnDelPujcka.setData(pujcka.getId());

			tblPujcky.addItem(
					new Object[] { pujcka.getBanka().getNazevBanky(),
							pujcka.getMesicniSplatka(), pujcka.getPocDatum(),
							pujcka.getKonDatum(),btnEditPujcka, btnDelPujcka }, null);
		}

	}
	
	public void reinitNewPujckaForm(){
		
		cbBanka.setValue(null);
		popupPocatek.setValue(null);
		popupKonec.setValue(null);
		tfMesicniSplatka.setValue("");
	}

	public void initNewPujckaForm() {
		btnBack = new Button("Zpet");
		btnBack.addListener(new BackButtonAction(this));

		btnSave = new Button("Uložit");
		btnSave.addListener(new SavePujckaAction(this));

		cbBanka = new ComboBox("Banka: ");
		popupKonec = new PopupDateField("Koncové datum: ");
		popupPocatek = new PopupDateField("Počáteční datum: ");
		tfMesicniSplatka = new TextField("Měsíční splátka: ");
		naplnComboboxy();
		formNewPujcka = new Form();
		formNewPujcka.setCaption("Nová půjčka: ");
		formNewPujcka.getLayout().addComponent(cbBanka);
		formNewPujcka.getLayout().addComponent(popupPocatek);
		formNewPujcka.getLayout().addComponent(popupKonec);
		formNewPujcka.getLayout().addComponent(tfMesicniSplatka);
		formNewPujcka.getLayout().addComponent(btnBack);
		formNewPujcka.getLayout().addComponent(btnSave);

		addComponent(formNewPujcka);
	}

	public void naplnComboboxy() {
		cbBanka.removeAllItems();
		for (Object o : DaoFactory.getinstance().getBanka()
				.getAll(0, Integer.MAX_VALUE)) {
			cbBanka.addItem(((com.autosalon.model.Banka) o).getNazevBanky());
		}
	}

	public ContentSwitcher getSwitcher() {
		return switcher;
	}

	public List<Object> getListPujcky() {
		return listPujcky;
	}

	public void setListPujcky(List<Object> listPujcky) {
		this.listPujcky = listPujcky;
	}

	public Button getBtnNewPujcka() {
		return btnNewPujcka;
	}

	public void setBtnNewPujcka(Button btnNewPujcka) {
		this.btnNewPujcka = btnNewPujcka;
	}

	public Table getTablePujcky() {
		return tblPujcky;
	}

	public void setTablePujcky(Table tablePujcky) {
		this.tblPujcky = tablePujcky;
	}

	public Form getFormNewPujcka() {
		return formNewPujcka;
	}

	public void setFormNewPujcka(Form formNewPujcka) {
		this.formNewPujcka = formNewPujcka;
	}

	public PopupDateField getPopupKonec() {
		return popupKonec;
	}

	public void setPopupKonec(PopupDateField popupKonec) {
		this.popupKonec = popupKonec;
	}

	public PopupDateField getPopupPocatek() {
		return popupPocatek;
	}

	public void setPopupPocatek(PopupDateField popupPocatek) {
		this.popupPocatek = popupPocatek;
	}

	public ComboBox getCbBanka() {
		return cbBanka;
	}

	public void setCbBanka(ComboBox cbBanka) {
		this.cbBanka = cbBanka;
	}

	public PagingComponent getPagingCompPujcky() {
		return pagingCompPujcky;
	}

	public void setPagingCompPujcky(PagingComponent pagingCompPujcky) {
		this.pagingCompPujcky = pagingCompPujcky;
	}

	public TextField getTfMesicniSplatka() {
		return tfMesicniSplatka;
	}

	public void setTfMesicniSplatka(TextField tfMesicniSplatka) {
		this.tfMesicniSplatka = tfMesicniSplatka;
	}

	public HorizontalLayout getButtonLayout() {
		return buttonLayout;
	}

	public void setButtonLayout(HorizontalLayout buttonLayout) {
		this.buttonLayout = buttonLayout;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Long getId_pujcky() {
		return id_pujcky;
	}

	public void setId_pujcky(Long id_pujcky) {
		this.id_pujcky = id_pujcky;
	}

}
